<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-inbound-edit" th:object="${inbound}">
            <input id="inboundId" name="inboundId" th:field="*{inboundId}"  type="hidden">
            <div class="form-group">	
                <label class="col-sm-3 control-label">入库日期：</label>
                <div class="col-sm-8">
                    <input id="inboundDate" name="inboundDate" th:field="*{inboundDate}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">物品名称：</label>
                <div class="col-sm-8">
					<select id="goodsId" name="goodsId" class="form-control select2-hidden-accessible" >
						<option th:each="goodsList:${goodsLists}" th:value="${goodsList.goodsId}" th:text="${goodsList.goodsName}" th:field="*{goodsId}"></option>
					</select>
				</div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">数量：</label>
                <div class="col-sm-8">
                    <input id="inboundNumber" name="inboundNumber" th:field="*{inboundNumber}" class="form-control" type="text" onkeyup="countMoney()">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">单价：</label>
                <div class="col-sm-8">
                    <input id="inboundPrice" name="inboundPrice" th:field="*{inboundPrice}" class="form-control" type="text" onkeyup="countMoney()">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">金额：</label>
                <div class="col-sm-8">
                    <input id="inboundMoney" name="inboundMoney" th:field="*{inboundMoney}" class="form-control" type="text" readonly="readonly">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input id="remark" name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div>
			<div class="form-group">
				<div class="form-control-static col-sm-offset-9">
					<button type="submit" class="btn btn-primary">提交</button>
					<button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
				</div>
			</div>
		</form>
    </div>
    <div th:include="include::footer"></div>
    <script th:src="@{/ajax/libs/select/select2.js}"></script>
    <script type="text/javascript">
    layui.use('laydate', function(){
		var laydate = layui.laydate;
		//执行一个laydate实例
		laydate.render({
			elem: '#inboundDate' //指定元素
		});
	});
    
    Date.prototype.Format = function (fmt) { 
        var o = {
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
            "S": this.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        	return fmt;
	} 

	$("#form-inbound-edit").validate({
		rules:{
			inboundDate:{
				required:true,
			},
			inboundNumber:{
				required:true,
			},
		},
		submitHandler: function(form) {
			edit();
		}
	});
	
	function edit() {
		var inboundId = $("input[name='inboundId']").val();
    	var inboundDate = $("input[name='inboundDate']").val();
    	inboundDate = new Date(inboundDate).Format("yyyy-MM-dd hh:mm:ss");
    	var goodsId = $.form.selectSelects("goodsId");
    	var inboundNumber = $("input[name='inboundNumber']").val();
    	var inboundPrice = $("input[name='inboundPrice']").val();
    	var inboundMoney = $("input[name='inboundMoney']").val();
    	var remark =  $("input[name='remark']").val();
    	$.ajax({
    		cache : true,
    		type : "POST",
    		url : ctx + "store/inbound/edit",
    		data : {
    			"inboundId":inboundId,
    			"inboundDate": inboundDate,
    			"goodsId": goodsId,
    			"inboundNumber" : inboundNumber,
    			"inboundPrice" : inboundPrice,
    			"inboundMoney" : inboundMoney,
    			"remark":remark
    		},
    		async : false,
    		error : function(request) {
    			$.modal.alertError("系统错误");
    		},
    		success : function(result) {
    			if (result.code == web_status.SUCCESS) {
                	$.operate.saveSuccess(result);
                } else {
                	$.modal.msgError(result.msg);
                }
    		}
    	});
    }
	
	function countMoney(){
    	var inboundPrice = $("input[name='inboundPrice']").val();
    	var inboundNumber = $("input[name='inboundNumber']").val();
    	var accountMoneyInput =  document.getElementById("inboundMoney");
    	accountMoneyInput.value = inboundPrice*inboundNumber;
    	
    }
	
	// h所有修改，光标默认定位在第1个输入框
	var oldVal=$('input:eq(1)').val();
	$('input:eq(1)').val("").focus().val(oldVal);
	</script>
</body>
</html>
